package top.jacktgq.concurrent_utils.pool;

import top.jacktgq.utils.LogUtils;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @Author CandyWall
 * @Date 2020/11/4--17:48
 * @Description 测试关闭线程池的两个方法
 */
public class ShutDownDemo {
    public static void main(String[] args) {
        //testShutdown();
        testShutdownNow();
    }

    public static void testShutdown(){
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        Future<?> result1 = threadPool.submit(() -> {
            LogUtils.log("testShutdown", "Task 1 running...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LogUtils.log("testShutdown", "Task 1 finish...");
            return 1;
        });

        Future<?> result2 = threadPool.submit(() -> {
            LogUtils.log("testShutdown", "Task 2 running...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LogUtils.log("testShutdown", "Task 2 finish...");
            return 2;
        });

        Future<?> result3 = threadPool.submit(() -> {
            LogUtils.log("testShutdown", "Task 3 running...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LogUtils.log("testShutdown", "Task 3 finish...");
            return 3;
        });
        //关闭线程池
        threadPool.shutdown();
        //下面这行在调用了shutdown()方法之后还会继续执行
        //shutdown()方法不是阻塞的，调用了以后不会等待线程池中的线程都停止后才会继续向下执行
        //调用awaitTermination()方法可以在线程池shutdown之后阻塞一段时间
        try {
            threadPool.awaitTermination(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LogUtils.log("testShutdown", "other...");

    }

    public static void testShutdownNow() {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        Future<?> result1 = threadPool.submit(() -> {
            LogUtils.log("testShutdownNow", "Task 1 running...");
            TimeUnit.SECONDS.sleep(1);
            LogUtils.log("testShutdownNow", "Task 1 finish...");
            return 1;
        });

        Future<?> result2 = threadPool.submit(() -> {
            LogUtils.log("testShutdownNow", "Task 2 running...");
            TimeUnit.SECONDS.sleep(1);
            LogUtils.log("testShutdownNow", "Task 2 finish...");
            return 2;
        });

        Future<?> result3 = threadPool.submit(() -> {
            LogUtils.log("testShutdownNow", "Task 3 running...");
            TimeUnit.SECONDS.sleep(1);
            LogUtils.log("testShutdownNow", "Task 3 finish...");
            return 3;
        });
        //关闭线程池
        List<Runnable> runnables = threadPool.shutdownNow();
        LogUtils.log("testShutdownNow", "other..." + runnables);
    }
}
